<template>
  <d2-container>
  <div class="mod-config">
    <div class="search">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList(true)">
        <el-form-item>
          <el-input v-model="dataForm.name" placeholder="活动名称" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList(true)">查询</el-button>
          <el-button v-if="isAuth('ecs:ecsrechargeactivity:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
          <el-button v-if="isAuth('ecs:ecsrechargeactivity:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      :data="dataList"
      border
      :height="containerHeight"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;margin-top:15px;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <!--<el-table-column-->
        <!--prop="activityId"-->
        <!--header-align="center"-->
        <!--align="center"-->
        <!--label="">-->
      <!--</el-table-column>-->
      <el-table-column
        prop="activityName"
        header-align="center"
        align="center"
        label="活动名称">
      </el-table-column>
      <el-table-column
        prop="realMoney"
        header-align="center"
        align="center"
        label="充值金额(元)">
      </el-table-column>
      <el-table-column
        prop="arriveMoney"
        header-align="center"
        align="center"
        label="赠送金额(元)">
      </el-table-column>
      <el-table-column
        prop="effectTime"
        header-align="center"
        align="center" min-width="100"
        label="生效时间">
      </el-table-column>
      <el-table-column
        prop="expireTime"
        header-align="center"
        align="center" min-width="100"
        label="失效时间">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="状态">
        <template slot-scope="scope">
          {{scope.row.status |statusName(statues)}}
        </template>
      </el-table-column>
      <el-table-column
        prop="activityDesc"
        header-align="center"
        align="center"
        label="活动描述">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <!--<el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.activityId)">修改</el-button>-->
          <el-button type="text" size="small" @click="updateState('2',scope.row.activityId)" v-if="scope.row.status==='1'">下架</el-button>
          <el-button type="text" size="small" @click="updateState('1',scope.row.activityId)" v-if="scope.row.status==='2'">上架</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.activityId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
  </d2-container>
</template>

<script>
  import * as Util from "@/utils";
  import AddOrUpdate from './ecsrechargeactivity-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          name: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        statues:[{"label":"上架","value":"1"},{"label":"下架","value":"2"} ]
      }
    },
    computed:{
      containerHeight() {
        return Util.getClientHeight() - 300;
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    filters: {
      statusName: function (value,statues) {
        for(let i=0;i<statues.length;i++){
          if(statues[i].value===value){
            return statues[i].label;
          }
        }
      }

    },
    methods: {
      // 获取数据列表
      getDataList (type) {
        if(type){
          this.pageIndex = 1
        }
        this.dataListLoading = true
        this.$api.okhttp({
          url: this.$http.adornUrl('/ecs/ecsrechargeactivity/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'name': this.dataForm.name,
            "customerId": this.$util.cookies.get('customer_id'),
          })
        }).then((data) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },

      updateState(state,activityId){
        this.$api.okhttp({
          url: this.$http.adornUrl(`/ecs/ecsrechargeactivity/update`),
          method: 'post',
          data: this.$http.adornData({
            'activityId': activityId ,
            'status': state,
            "customerId": this.$util.cookies.get('customer_id'),
          })
        }).then((data) => {
          if (data && data.code === 0) {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
            this.visible = false
            this.getDataList()
        }
        })
        } else {
          this.$message.error(data.msg)
        }
      })

      },

      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.activityId
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$api.okhttp({
            url: this.$http.adornUrl('/ecs/ecsrechargeactivity/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then((data) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
